using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class ClipNumber:MonoBehaviour {
	void Start() {
		text=GetComponent<TextMeshProUGUI>();
	}

	static Color colorNormal = Color.white;
	static Color colorUnderHalf = new Color(217f/256f,98f/256f,0);
	static Color colorLow = new Color(171f/256f,29f/256f,10f/256f);
	static Color colorEmpty1 = new Color(171f/256f,29f/256f,10f/256f);
	static Color colorEmpty2 = new Color(38f/256f,25f/256f,23f/256f);
	const float colorChangeTime = 0.15f;

	float colorChangeTimer;
	bool colorToUse;

	int previousClipSize=-1;
	int previousAmmoCount=-1;

	void Update() {

		ClipStats clipToDisplay = Player.instance.activeWeapon.weapon.currentClip;

		int clipSize = Mathf.CeilToInt(clipToDisplay.clipSize);
		int ammoCount = clipToDisplay.content.Count;

		colorChangeTimer+=Time.deltaTime;
		if(colorChangeTimer>colorChangeTime) {
			colorToUse=!colorToUse;
			colorChangeTimer-=colorChangeTime;
		}

		if(ammoCount>=Mathf.Min((clipSize+1)/2,10)) text.color=colorNormal;
		else if(ammoCount>=Mathf.Min((clipSize+3)/4,5)) text.color=colorUnderHalf;
		else if(ammoCount>0) text.color=colorLow;
		else text.color=colorToUse ? colorEmpty1 : colorEmpty2;

		if(clipSize!=previousClipSize||ammoCount!=previousAmmoCount) {
			previousAmmoCount=ammoCount;
			previousClipSize=clipSize;
			text.text=ammoCount+"/"+clipSize;
		}

	}

	TextMeshProUGUI text;
}
